// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  // return cloud.database().collection("users").add({
  //   data: {
  //     openId: event.openId,
  //     signType: event.signType,
  //     name: event.name,
  //     sex: event.sexType,
  //     cardType: event.cardType,
  //     cardId: event.cardId,
  //     phone: event.phone,
  //     school: event.school,
  //     city: event.city,
  //     email: event.email
  //   },
  //   success: sres => {
  //     console.log("插入数据成功", sres)
  //   },
  //   fail: sres => {
  //     console.log("插入数据失败", sres)
  //   }
  // })



  // cloud.callFunction({
  //   // 要调用的云函数名称
  //   name: 'getUserByOpenId',
  //   // 传递给云函数的参数
  //   data: {
  //     openId: event.openId
  //   },
  //   success: res => {
  //     console.log("调用云函数获取用户信息成功", res)
  //     if (res.result.data.length != 0) {
  //       // console.log(res.result.data[0])
  //       return cloud.database().collection("users").where({
  //         openId: event.openId
  //       }).update({
  //         data: {
  //           signType: event.signType,
  //           name: event.name,
  //           sex: event.sexType,
  //           cardType: event.cardType,
  //           cardId: event.cardId,
  //           phone: event.phone,
  //           school: event.school,
  //           city: event.city,
  //           email: event.email
  //         },
  //         success: sres => {
  //           console.log("更新数据成功", sres)
  //         },
  //         fail: rses => {
  //           console.log("更新数据失败", sres)
  //         }
  //       })
  //     } else {
  //       return cloud.database().collection("users").add({
  //         data: {
  //           openId: event.openId,
  //           signType: event.signType,
  //           name: event.name,
  //           sex: event.sexType,
  //           cardType: event.cardType,
  //           cardId: event.cardId,
  //           phone: event.phone,
  //           school: event.school,
  //           city: event.city,
  //           email: event.email
  //         },
  //         success: sres => {
  //           console.log("插入数据成功", sres)
  //         },
  //         fail: sres => {
  //           console.log("插入数据失败", sres)
  //         }
  //       })
  //     }
  //     // console.log(that.data.user, "woxiaole")
  //   },
  //   fail: res => {
  //     console.log("调用云函数获取用户信息失败", res)
  //   }
  // })

  const res = await cloud.callFunction({
    // 要调用的云函数名称
    name: 'getUserByOpenId',
    // 传递给云函数的参数
    data: {
      openId: event.openId
    },
    success: res => {
      console.log("调用云函数获取用户信息成功", res)
    },
    fail: res => {
      console.log("调用云函数获取用户信息失败", res)
    }
  })
  console.log("shiwo", res)
  if (res.result.data.length != 0) {
    return cloud.database().collection("users").where({
      openId: event.openId
    }).update({
      data: {
        // signType: event.signType,
        name: event.name,
        sex: event.sexType,
        cardType: event.cardType,
        cardId: event.cardId,
        phone: event.phone,
        school: event.school,
        city: event.city,
        email: event.email
      },
      success: sres => {
        console.log("更新数据成功", sres)
      },
      fail: sres => {
        console.log("更新数据失败", sres)
      }
    })
  } else {
    return cloud.database().collection("users").add({
      data: {
        openId: event.openId,
        signType: event.signType,
        name: event.name,
        sex: event.sexType,
        cardType: event.cardType,
        cardId: event.cardId,
        phone: event.phone,
        school: event.school,
        city: event.city,
        email: event.email
      },
      success: sres => {
        console.log("插入数据成功", sres)
      },
      fail: sres => {
        console.log("插入数据失败", sres)
      }
    })
  }
}